package model;


import javax.swing.DefaultListModel;

import cs.remoteapp.Message;

/**
 * Class representing a conversation between the local user and a remote user
 * Package  model
 * Project ChatSystemG5 - INSA 4INFOA1 - 2013/2014
 * @author Thomas Vuillemin Martin Conant
 */
public class Conversation {
	private DefaultListModel<Message> history;
	//The remote User
	private User user;
	
	/**
	 * Constructor of Conversation
	 * Instanciate a defaultListModel of Message
	 * @param user the remote user
	 */
	public Conversation(User user){
		history = new DefaultListModel<Message>();
		this.user=user;
	}
	
	/**
	 * Get the remote user
	 * @return the remote User of the conversation
	 */
	public User getUser() {
		return user;
	}
	

	/**
	 * Set the remote user
	 * @param user the remote user of the conversation
	 */
	public void setUser(User user) {
		this.user = user;
	}

	/**
	 * get the history of exchanged message
	 * @return the DefaultListModel containing the history of exchanged messages
	 */
	public DefaultListModel<Message> getHistory() {
		return this.history;
	}
	
	/**
	 * Add a message at the end of the history
	 * @param message the message to add
	 */
	public void addMessage(Message message){
		history.addElement(message);
	}
	
	/**
	 * Search for a fileMessage in the conversation
	 * @param name name of the file to search
	 * @return the index of the fileMessage if found, -1 if not found
	 */
	public int findFileMessage(String name){
		for (int i=0; i < history.getSize(); i++) {
			if (history.get(i) instanceof FileMessage){
				//System.out.println("found a fileMessage\n");
				//System.out.println(((FileMessage)(history.get(i))).getFile().getName() + " =? "+ name+ "\n");
				if ( (((FileMessage)(history.get(i))).getFile().getName()).equals(name) ){
					return i;
				}
			}
		}
		return -1;
	}
	
	
	
	
}
